package com.jmpt.fangbohui.ui.personalcenter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.adapter.SecondHouseOrderAdapter;
import com.jmpt.fangbohui.bean.SecondHouseOrderBean;
import com.jmpt.fangbohui.bean.SecondHouseOrderBean.Data;
import com.jmpt.fangbohui.dao.LoginSQLiteOperator;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.ui.base.BasePersonalCenterUserActivity;
import com.jmpt.fangbohui.utils.DebugLog;
import com.jmpt.fangbohui.utils.ToastHelper;

//经纪人我的订单
public class BrokerMyIndentActivity extends BaseHeadActivity {


	private ListView lv_my_indent;
	private TextView tv_my_indent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_broker_my_indent);
		initView();
		getDataForNet();
	}
	
	/**
	 * 
	 * getDataForNet:到网上获取订单信息. <br/>
	 * SECONDHOUSEORDER
	 * @author Lee
	 * @since JDK 1.7
	 */
	private void getDataForNet() {
		LoginSQLiteOperator oper = new LoginSQLiteOperator(this);
		final List<String> querySessionId = oper.querySessionId();
		if(querySessionId.size() != 2){
			return;
		}
		
		new Thread(){
			public void run() {
				HttpClient httpClient = new DefaultHttpClient();
				try {
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("AgentId",querySessionId.get(0)));
					
				    HttpPost postMethod = new HttpPost(Constants.URL.ADDCLIENTURL);
				    postMethod.setHeader("Cookie", "PHPSESSID=" + querySessionId.get(1)); 
					DebugLog.i("AgentId"+querySessionId.get(0));
					DebugLog.i("PHPSESSID=" + querySessionId.get(1));
				    
				    postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
								
				    HttpResponse response = httpClient.execute(postMethod); //执行POST方法
				    
				    if(response.getStatusLine().getStatusCode() != 200){
				    	ToastHelper.ShowToast("网络不畅通，请稍后再试", BrokerMyIndentActivity.this);
				    	return;
				    }
				    parsers(EntityUtils.toString(response.getEntity(), "utf-8"));
				    
								
				} catch (Exception e) {
				    e.printStackTrace();
				} 
			};
		}.start();

	}

	protected void parsers(String result) {
		DebugLog.i(result);
		Gson gson = new Gson();
		final SecondHouseOrderBean bean = gson.fromJson(result, SecondHouseOrderBean.class);
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				if(bean.done.equals("true")){
					
					List<Data> data = bean.retval.data;
					SecondHouseOrderAdapter mAdapter = new SecondHouseOrderAdapter(BrokerMyIndentActivity.this,data);
					lv_my_indent.setAdapter(mAdapter);
					tv_my_indent.setVisibility(View.GONE);
					lv_my_indent.setVisibility(View.VISIBLE);
				}else{
					tv_my_indent.setVisibility(View.VISIBLE);
					lv_my_indent.setVisibility(View.GONE);
				}
			}
		});
	}

	protected void initView() {
		showTitle("我的订单");
		showBackButton(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		lv_my_indent = (ListView) findViewById(R.id.lv_my_indent);
		tv_my_indent = (TextView) findViewById(R.id.tv_my_indent);
		
	}

}
